package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "公共接口")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 文件上传功能
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传接口")
    public Result upload(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        String realFilename = file.getOriginalFilename();
        String suffix = realFilename.substring(realFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID() + suffix;
        String url = aliOssUtil.upload(inputStream, fileName);
        return Result.success(url);
    }
}
